package com.imi.sdk.jnitest

import android.content.Context
import android.util.Log
import com.hjimi.api.iminect.*

class Camera(val ctx:Context, val W:Int = 640,val H:Int = 480) {

    private var imiDevice: ImiDevice? = null
    private var imiCamera: ImiCamera? = null
    private var mImiNect: ImiNect? = null

    internal var depType = ImiFrameType.DEPTH

    var isConfiged = false

    var onEvent:((Int,String)->Unit)? = null

    fun onProcEvent(ev:Int,msg:String=""){
        onEvent?.invoke(ev,msg)
    }

    fun release(){
        imiCamera?.close()
        imiDevice?.close()
    }

    @Throws(Exception::class)
    fun initWithCamera() {
//        if (!isConfiged) {
//            throw Exception("It is not setConfig yet")
//        }

        mImiNect = ImiNect.create(ctx)
        imiDevice = mImiNect!!.Device()

        //
        imiDevice!!.open(object : ImiDevice.OpenDeviceListener {
            override fun onOpenDeviceSuccess() {
                imiCamera = mImiNect!!.Camera()
                imiCamera!!.open(object : ImiCamera.OpenCameraListener {
                    override fun onOpenCameraSuccess() {
                        //
                        startCameraPreview()
                    }

                    override fun onOpenCameraFailed(s: String) {
                        onProcEvent(0,"OpenCameraFailed")
                    }
                })
            }

            override fun onOpenDeviceFailed(s: String) {
                onProcEvent(0,"OpenDeviceFailed")
            }
        })
    }


    private fun startCameraPreview() {
        imiDevice!!.openStream(depType)

        //        imiCamera.setCameraMirrorCapability(false);
        //

        val mode = ImiCameraFrameMode(ImiCameraPixelFormat.IMI_CAMERA_PIXEL_FORMAT_RGB888, W, H, 30)
        val errcode = imiCamera!!.startPreview(mode)

        //val depType =
        val frameMode = ImiFrameMode(ImiPixelFormat.IMI_PIXEL_FORMAT_DEP_16BIT, W, H)
        val rc = imiDevice!!.setFrameMode(depType, frameMode)

        val err = imiDevice!!.imiGetErrorString(rc)

        val hDev = imiDevice!!.getDeviceHandle()
        val hCam = imiCamera!!.getCameraHandle()
        val hDepStream = imiDevice!!.getStreamHandle(depType)

        //
        //        boolean ok =

        //        if (ok) {
        try {

            if(hDev!=0 && hCam!=0 && hDepStream!=0) {
//TODO
                //initCamera(hDev, hCam, hDepStream)
                //
                //
                bindHandle()
                onProcEvent(1)
            }else{
                Log.e(">>","start FAIL")
                onProcEvent(0,"hDev=$hDev && hCam=$hCam && hDepStream=$hDepStream \n =>$rc: ${err}")
            }
        } catch (e: Exception) {
            onProcEvent(0,e.message?:"")
        }
    }

    fun bindHandle(){
//        val depType = ImiFrameType.USER_INDEX_SKELETON
        val hDev = imiDevice?.getDeviceHandle()?:0
        val hCam = imiCamera?.getCameraHandle()?:0
        val hDepStream = imiDevice?.getStreamHandle(depType)?:0
        //
        (ctx as MainActivity).setHandles(hCam.toLong(),hDev.toLong(),hDepStream.toLong())
    }

    //onInitHandles:(Long,Long,Long)->Unit
}
